/**
 * 
 */
package fr.toussna.managedbean;

import java.util.ArrayList;
import java.util.List;

import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;
import javax.servlet.http.HttpServletRequest;

import fr.toussna.entity.Article;
import fr.toussna.entity.Member;
import fr.toussna.service.impl.AccountServiceImpl;
import fr.toussna.service.impl.ArticleServiceImpl;

/**
 * BackingBean de JSF gère les membres et les comptes utilisateurs
 * 
 * @author My
 * @version 2.0
 * 
 */
public class MemberManagedBean {

	// **********************************
	// * Attributs *
	// **********************************

	/**
	 * @return the articleservice
	 */
	public ArticleServiceImpl getArticleservice() {
		return articleservice;
	}

	/**
	 * @return the articles
	 */
	public List<Article> getArticles() {
		articles = articleservice.findArticleOfMember(member);
		return articles;
	}

	/**
	 * 
	 */
	private ArticleServiceImpl articleservice;

	/**
	 * @param articleservice
	 *            the articleservice to set
	 */
	public void setArticleservice(ArticleServiceImpl articleservice) {
		this.articleservice = articleservice;
	}

	List<SelectItem> civilityList;
	{
		fillSelectItems();
	}
	/**
	 * Un membre {@link Member}
	 */
	private Member member;

	private List<Article> articles;

	/**
	 * @param articles
	 *            the articles to set
	 */
	public void setArticles(List<Article> articles) {
		this.articles = articles;
	}

	/**
	 * L'article appartenant au membre
	 */
	private Article article;

	private String email;
	private String password2;
	private String password;
	private String name;
	private String address;
	private String firstName;
	private String birthDay;
	private String selectedCivility;

	/**
	 * @see AccountServiceImpl
	 */

	private AccountServiceImpl accountservice;

	/**
	 * Si le membre possede un compte ou pas
	 */
	private String exist = null;

	// **********************************
	// * Navigation Methodes *
	// **********************************

	/**
	 * 
	 */
	public String logOut() {
		HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
		request.getSession().invalidate();
		articles = null;
		return null;
	}

	/**
	 * Connecte un client vers son compte
	 */
	public String logOn() {

		if (accountservice.IdentifyWebClient(new Member(email, password2))) {
			member = accountservice.findMemberByName(email).get(0);
			HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
			request.getSession().setAttribute("member", member);

			articles = articleservice.findArticleOfMember(member);

			return null;
		} else
			return "failed";
	}

	/**
	 * Crée un nouveau compte pour un client web
	 * 
	 * @return
	 */
	public String createAccount() {

		if (!password.equals(password2)) {
			exist = "les mot de passes sont differents";
			return "new";
		}
		List<Member> list = accountservice.findMemberByName(email);
		if (!list.isEmpty() && list.get(0).getEmail().equals(email)) {
			exist = "Il existe deja un compte avec ces parametres";
			return "new";
		} else {
			member = new Member(email, password, selectedCivility, name, firstName, birthDay, address, null);
			accountservice.createMember(member);
			exist = null;
			logOn();
			return "success";
		}
	}

	/**
	 * Redirige vers la page d'authentification
	 * 
	 * @return
	 */
	public String wantToSignIn() {
		return "signin";
	}

	/**
	 * Redirige vers la page de creation d'un compte pour un nouveau membre
	 * 
	 * @return
	 */
	public String wantToCreateAccount() {

		return "new";
	}

	// **********************************
	// * Private Methodes *
	// **********************************

	private void fillSelectItems() {
		civilityList = new ArrayList<SelectItem>();
		civilityList.add(new SelectItem("Mme"));
		civilityList.add(new SelectItem("Mr"));
		civilityList.add(new SelectItem("Mlle"));
	}

	/**
	 * 
	 */
	private String getParameter(String name) {
		HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
		return request.getParameter(name);
	}

	// **********************************
	// * Getters & Setters *
	// **********************************

	/**
	 * @return l'article
	 */
	public Article getArticle() {
		return article;
	}

	/**
	 * @return the selectedCivility
	 */
	public String getSelectedCivility() {
		return selectedCivility;
	}

	/**
	 * @param selectedCivility
	 *            the selectedCivility to set
	 */
	public void setSelectedCivility(String selectedCivility) {
		this.selectedCivility = selectedCivility;
	}

	/**
	 * @return the civilityList
	 */
	public List<SelectItem> getCivilityList() {
		return civilityList;
	}

	/**
	 * @param civilityList
	 *            the civilityList to set
	 */
	public void setCivilityList(List<SelectItem> civilityList) {
		this.civilityList = civilityList;
	}

	/**
	 * @return the exist
	 */
	public String getExist() {
		return exist;
	}

	/**
	 * @param exist
	 *            the exist to set
	 */
	public void setExist(String exist) {
		this.exist = exist;
	}

	/**
	 * @return the accountservice
	 */
	public AccountServiceImpl getAccountservice() {
		return accountservice;
	}

	/**
	 * @param accountservice
	 *            the accountservice to set
	 */
	public void setAccountservice(AccountServiceImpl accountservice) {
		this.accountservice = accountservice;
	}

	/**
	 * @return the email
	 */
	public String getEmail() {
		return email;
	}

	/**
	 * @param email
	 *            the email to set
	 */
	public void setEmail(String email) {
		this.email = email;
	}

	/**
	 * @return the password
	 */
	public String getPassword2() {
		return password2;
	}

	/**
	 * @param password
	 *            the password to set
	 */
	public void setPassword2(String password) {
		this.password2 = password;
	}

	/**
	 * @param article
	 *            l'article a setter
	 */
	public void setArticle(Article article) {
		this.article = article;
	}

	/**
	 * @return le member
	 */
	public Member getMember() {
		return member;
	}

	/**
	 * @param member
	 *            le member a setter
	 */
	public void setMember(Member member) {
		this.member = member;
	}

	/**
	 * @return the password
	 */
	public String getPassword() {
		return password;
	}

	/**
	 * @param password
	 *            the password to set
	 */
	public void setPassword(String password) {
		this.password = password;
	}

	/**
	 * @return the name
	 */
	public String getName() {
		return name;
	}

	/**
	 * @param name
	 *            the name to set
	 */
	public void setName(String name) {
		this.name = name;
	}

	/**
	 * @return the address
	 */
	public String getAddress() {
		return address;
	}

	/**
	 * @param address
	 *            the address to set
	 */
	public void setAddress(String address) {
		this.address = address;
	}

	/**
	 * @return the firstName
	 */
	public String getFirstName() {
		return firstName;
	}

	/**
	 * @param firstName
	 *            the firstName to set
	 */
	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}

	/**
	 * @return the birthDay
	 */
	public String getBirthDay() {
		return birthDay;
	}

	/**
	 * @param birthDay
	 *            the birthDay to set
	 */
	public void setBirthDay(String birthDay) {
		this.birthDay = birthDay;
	}

}
